import { computed } from 'vue'
import { createPopup } from '@/utils/popup'

const popups = {}

export function usePopup() {
  const setPopup = (key, popup) => {
    popups[key] = popup
  }

  const getPopupRef = (key) => {
    return computed(() => popups[key])
  }

  const getPopup = (key) => {
    return popups[key]
  }

  const removePopup = (key) => {
    const popup = popups[key]
    if (popup) {
      popup.destroy()
      delete popups[key]
    }
  }

  const clear = () => {
    for (let key in popups) {
      removePopup(key)
    }
  }

  return { getPopup, setPopup, getPopupRef, createPopup, removePopup, clear }
}
